<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Creates a collection</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongodb.construct.html">MongoDB::__construct</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.createdbref.html">MongoDB::createDBRef</a></div>
 <div class="up"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongodb.createcollection" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB::createCollection</h1>
  <p class="verinfo">(PECL mongo &gt;=0.9.0)</p><p class="refpurpose"><span class="refname">MongoDB::createCollection</span> &mdash; <span class="dc-title">Creates a collection</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-mongodb.createcollection-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.mongocollection.html" class="type MongoCollection">MongoCollection</a></span> <span class="methodname"><strong>MongoDB::createCollection</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span>
  ] )</div>

  
  <p class="para rdfs-comment">
   This method is used to create capped collections and other collections 
   requiring special options.  It is identical to running:
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">command</span><span style="color: #007700">(array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"create"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"capped"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">"capped"</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"size"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">"size"</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"max"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">"max"</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"autoIndexId"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">"autoIndexId"</span><span style="color: #007700">],<br />));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   See <span class="function"><a href="mongodb.command.html" class="function">MongoDB::command()</a></span> for more information about database commands.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb.createcollection-parameters">
  <h3 class="title">参数</h3>

  <p class="para">
   <dl>

    
     <dt>

      <em><code class="parameter">name</code></em>
     </dt>

     <dd>

      <p class="para">
       The name of the collection.
      </p>
     </dd>

       

    
     <dt>

      <em><code class="parameter">options</code></em>
     </dt>

     <dd>

      <p class="para">
       An array containing options for the collections. Each option is its own
       element in the options array, with the option name listed below being
       the key of the element. The supported options depend on the MongoDB
       server version. At the moment, the following options are supported:
      </p>
      <p class="para">
       <dl>

        
         <dt>

          <em><code class="parameter">capped</code></em>
         </dt>

         <dd>

          <p class="para">
           If the collection should be a fixed size.
          </p>
         </dd>

           
        
         <dt>

          <em><code class="parameter">size</code></em>
         </dt>

         <dd>

          <p class="para">
           If the collection is fixed size, its size in bytes.
          </p>
         </dd>

           
        
         <dt>

          <em><code class="parameter">max</code></em>
         </dt>

         <dd>

          <p class="para">
           If the collection is fixed size, the maximum number of elements to
           store in the collection.
          </p>
         </dd>

           
        
         <dt>

          <em><code class="parameter">autoIndexId</code></em>
         </dt>

         <dd>

          <p class="para">
           If capped is <strong><code>TRUE</code></strong> you can specify <strong><code>FALSE</code></strong> to disable the
           automatic index created on the <em>_id</em> field.
           Before MongoDB 2.2, the default value for
           <em>autoIndexId</em> was <strong><code>FALSE</code></strong>.
          </p>
         </dd>

           
       </dl>

      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongodb.createcollection-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a collection object representing the new collection.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-mongodb.createcollection-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1385">
   <p><strong>Example #1 <span class="function"><strong>MongoDB::createCollection()</strong></span> capped collection example</strong></p>
   <div class="example-contents"><p>
    A capped collection is a special type of collection that has either a fixed
    or a fixed number of elements. Once the collection is &quot;full,&quot; the oldest
    elements will be removed when new elements are added.  Capped collections
    can be very useful for applications like logging, where you may want to
    reserve a certain amount of space for logs and not worry about them
    getting too big.
   </p></div>
   <div class="example-contents"><p>
    This example creates a very tiny log collection that will keep a maximum of
    10 documents.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$log&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createCollection</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"logger"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'capped'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'size'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">*</span><span style="color: #0000BB">1024</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'max'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">10<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />);<br /><br />for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$log</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"level"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">WARN</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"msg"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"sample&nbsp;log&nbsp;message&nbsp;#</span><span style="color: #0000BB">$i</span><span style="color: #DD0000">"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"ts"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDate</span><span style="color: #007700">()));<br />}<br /><br /></span><span style="color: #0000BB">$msgs&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$log</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">();<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$msgs&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$msg</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$msg</span><span style="color: #007700">[</span><span style="color: #DD0000">'msg'</span><span style="color: #007700">].</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen"><br />
sample log message #90<br />
sample log message #91<br />
sample log message #92<br />
sample log message #93<br />
sample log message #94<br />
sample log message #95<br />
sample log message #96<br />
sample log message #97<br />
sample log message #98<br />
sample log message #99<br />
   </div>
  </div>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongodb.createcollection-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>1.4.0</td>
       <td>
        <p class="para">
         In versions before 1.4.0, the options were all arguments to the method. The
         function synopsis in those older versions is:
        </p>
        <p class="para">
         <div class="methodsynopsis dc-description">
          <span class="modifier">public</span> <span class="type"><a href="class.mongocollection.html" class="type MongoCollection">MongoCollection</a></span> <span class="methodname"><strong>MongoDB::createCollection</strong></span>
           ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
          [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$capped</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span>
          [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code><span class="initializer"> = 0</span></span>
          [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$max</code><span class="initializer"> = 0</span></span>
         ]]] )</div>

        </p>
        <p class="para">
         The meaning of the options is the same as described under the
         <em><code class="parameter">options</code></em> argument above.
        </p>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongodb.construct.html">MongoDB::__construct</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.createdbref.html">MongoDB::createDBRef</a></div>
 <div class="up"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
